package com.ruoyi.advice;


import com.ruoyi.log.domain.OperationLog;
import com.ruoyi.log.mapper.OperationLogMapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect
public class LogAdvice {
    OperationLog log=new OperationLog();
    @Autowired
    OperationLogMapper  operationLogMapper;
    @Pointcut("execution(* com.ruoyi.*.service.impl.*.de*(..))")
    public void op(){}

    @Around("op()")
    public Object log(ProceedingJoinPoint pjp){
        Object result=null;
        String exceptionType="";
        String message="";
        long time=0;
        String methodName = pjp.getSignature().getName();
        Object args[]=pjp.getArgs();
        try {
            long startTime = System.currentTimeMillis();
            result= pjp.proceed(args);
            long endTime = System.currentTimeMillis();
            time = endTime - startTime;
        } catch (Throwable e) {
            // 获取异常类型
            exceptionType = e.getClass().getName();
            message = e.getMessage();

        }
        log.setExceptionMessage( message);
        log.setExceptionType(exceptionType);
        log.setExecutionTime(time);
        log.setMethodName(methodName);
        log.setReturnValue( result+"");
        operationLogMapper.insertOperationLog( log);



        return result;
    }
}
